
#include <pic.h>
#include <htc.h> 
#include <stdio.h>
#include <stdlib.h>
#include "Init.h"
#include "SysClock.h"
#include "my_macros.h"
#include "App.h"
#include "outputs.h"
#include "hardware.h"

#define   APP_START	0
#define   APP_MAIN	1


static unsigned char app_state = APP_START;

#define   TIME_15_MIN		900

unsigned int i, seed;
unsigned int cnt;

unsigned int get_random ( void )
{
	unsigned int num;
	unsigned char i, cnt = 0;

	for(;;)
	{
		num = rand();

		for(i=0; i<15; i++)
		{
			if ( num & ((unsigned int)1<<i) )
			{
				if ( ++cnt > 4 )
				{
					return num;
				}
			}
		}
	}

	//return num;
}

void App ( void )
{
	seed++;

	if ( Time_500ms )
	{
		LED ^= 1;
	}

	switch( app_state )
	{
		case APP_START:
			if ( Time_5000ms )
			{	
				app_state = APP_MAIN;
				Out_Status = get_random();
			}
			break;

		case APP_MAIN:
			
			if ( TEST_BTN == 0 && Time_5000ms )
			{
				srand(seed);

				Out_Status = get_random();
			}
			
			if ( Time_1000ms && ++cnt == TIME_15_MIN )
			{
				cnt = 0;
				Out_Status = get_random();
			}			
			
			break;
	}  
}
